<template>
    <label :class="wrapClasses">
        <input v-if="!group && name" type="hidden" :name="name" v-model="checkedValue">
        <span :class="checkboxClasses">
            <span :class="innerClasses"></span>
            <input
                    v-if="group"
                    type="checkbox"
                    :class="inputClasses"
                    :disabled="disabled"
                    :value="value"
                    v-model="model"
                    @click="toggle">
            <input
                    v-if="!group"
                    type="checkbox"
                    :class="inputClasses"
                    :disabled="disabled"
                    :checked="currentValue"
                    @click="toggle">
        </span>
        <slot><span v-if="showSlot">{{ label }}</span></slot>
    </label>
</template>
<script>
    import {findComponentUpward} from '../../utils/assist';
    import Emitter from '../../mixins/emitter';

    const prefixCls = 'ivu-checkbox';

    export default {
        name: 'Checkbox',
        mixins: [Emitter],
        props: {
            disabled: {
                type: Boolean,
                default: false
            },
            name: String,
            data: [String, Array],
            value: {
                type: [Boolean, String, Number],
                default: false
            },
            label: String,
            indeterminate: {
                type: Boolean,
                default: false
            }
        },
        data () {
            return {
                model: [],
                group: false,
                showSlot: true,
                dataArray: [],
                checkedValue: null,
                currentValue: null,
                parent: findComponentUpward(this, 'CheckboxGroup')
            };
        },
        computed: {
            wrapClasses () {
                return [
                    `${prefixCls}-wrapper`,
                    {
                        [`${prefixCls}-group-item`]: this.group,
                        [`${prefixCls}-wrapper-checked`]: this.currentValue,
                        [`${prefixCls}-wrapper-disabled`]: this.disabled
                    }
                ];
            },
            checkboxClasses () {
                return [
                    `${prefixCls}`,
                    {
                        [`${prefixCls}-checked`]: this.currentValue,
                        [`${prefixCls}-disabled`]: this.disabled,
                        [`${prefixCls}-indeterminate`]: this.indeterminate
                    }
                ];
            },
            innerClasses () {
                return `${prefixCls}-inner`;
            },
            inputClasses () {
                return `${prefixCls}-input`;
            }
        },
        mounted () {
            this.currentValue = eval(this.value);

            this.parent = findComponentUpward(this, 'CheckboxGroup');
            if (this.parent) this.group = true;
            if (!this.group) {
                //
                if (!this.data) {
                    this.dataArray = [1, 0];
                } else if (Array.isArray(this.data) && this.data.length >= 2) {
                    this.dataArray = this.data;
                } else if (this.data.indexOf(",") > 0 && this.data.split(",").length >= 2) {
                    this.dataArray = this.data.split(",");
                } else {
                    this.dataArray = [1, 0];
                }

                this.updateModel();
                this.showSlot = this.$slots.default !== undefined;
            } else {
                this.parent.updateModel(true);
            }
        },
        methods: {
            toggle () {
                if (this.disabled) {
                    return false;
                }

                const checked = !this.currentValue;
                this.currentValue = checked;
                this.$emit('input', checked);

                if (this.group) {
                    this.$parent.change(this.model);
                } else {
                    if (this.currentValue) {
                        this.checkedValue = this.dataArray[0];
                    } else {
                        this.checkedValue = this.dataArray[1];
                    }

                    this.$emit('on-change', checked);
                    this.dispatch('FormItem', 'on-form-change', checked);
                }
            },
            updateModel () {
                this.currentValue = this.value;
                if (!this.group) {
                    if (this.currentValue) {
                        this.checkedValue = this.dataArray[0];
                    } else {
                        this.checkedValue = this.dataArray[1];
                    }
                }
            }
        },
        watch: {
            value () {
                this.updateModel();
            }
        }
    };
</script>
